<?php
/*
 * ---------------------------------------------------------------
* Author  : Anthony Tran
* Email   : Incredibletran@gmail.com - Incredibletran@hotmail.com
* Version : 1.2
* ---------------------------------------------------------------
*/
include APPPATH.'controllers/admin_controller'.EXT;
class Gallery_group extends Admin_controller{
	function __construct(){
		parent::__construct();
		$this->load->model(array('group_model'));
		$this->load->library(array('session','upload'));
		$this->load->helper(array('link','upload','text'));

		if($this->session->userdata('language')){
			$language   =   $this->session->userdata('language');
		}else{
			$language  =   '';
		}

		if ($language == LANG) {
			$this->lang->load(LANG, LANG_FOLDER);
		}else{
			$this->lang->load(LANG_DEFAULT, LANG_FOLDER_DEFAULT);
		}
	}
	
	
	/* function index(){
		
		#get number item on perpage.
		if(isset($_REQUEST['perpage'])) {
			$perpage		= 	$_REQUEST['perpage'];
			$this->session->set_userdata('group_perpage',$perpage);
		} else {
			if($this->session->userdata('group_perpage')) {
				$perpage	= 	$this->session->userdata('group_perpage');
			} else {
				$perpage 	= 	'all';
				$this->session->set_userdata('group_perpage',$perpage);
			}
		}
		
		#get total item in DB.
		$total_page					= 	count($this->group_model->get_all());
		$this->load->library('pagination');
		$config['total_rows'] 		= 	$total_page;
		$config['per_page'] 		= 	$perpage;
		$config['first_link']		= 	$this->lang->line('first');
		$config['last_link']		= 	$this->lang->line('last');
		$config['next_link']		= 	$this->lang->line('next');
		$config['prev_link']		= 	$this->lang->line('prev');
		$config['first_tag_open']	= 	'<span>';
		$config['first_tag_close']	= 	'</span>';
		$config['last_tag_open']	= 	'<span>';
		$config['last_tag_close']	= 	'</span>';
		$config['num_tag_open']		= 	'<span style="padding:0 2px 0 2px">';
		$config['num_tag_close']	= 	'</span>';
		$config['num_links']		= 	4;
		$config['cur_tag_open']		= 	'<a class="currentpage">';
		$config['cur_tag_close']	= 	'</a>';
		$config['base_url']			= 	admin_url().'/gallery-cate/';
		$config['uri_segment']		= 	3;
		$this->pagination->initialize($config);
		
		$pagination					= 	$this->pagination->create_links();
		$offset 					= 	($this->uri->segment(3)=='') ? 0 : $this->uri->segment(3);
		
		#get data.
		$group_list					=	$this->group_model->get_list($perpage, $offset);
		$group_url					=	$this->selfURL();
		$this->session->set_userdata('group_url', $group_url);
		$delete_group				=	$this->session->userdata('delete_group');
		$this->session->unset_userdata('delete_group');
		
		if(isset($delete_group) && $delete_group == 'delete group'){
			$inform	=	'delete group success';
		}else {
			$inform = "";
		}
		
		$data['current_perpage']	=	$perpage;
		$data['pagination']			=	$pagination;
		$data['current_url']		=	$group_url;
		$data['group_list']			=	$group_list;
		$data['inform']				=	$inform;
		$data['act']				=	"album";
		$data['tpl_file']			=	"group_index";
		$this->load->view('admin/admin_layout/index', $data);
	} */
	
	
	function add(){
		if($_SERVER['REQUEST_METHOD'] == "POST"){
			$this->form_validation->set_rules('name', $this->lang->line('name'), 'required|trim|xss_clean');
			$this->form_validation->set_rules('description', $this->lang->line('description'), 'trim|xss_clean');
			if($this->form_validation->run() == TRUE){
				$info					=	array();
				$info['name']			=	$this->input->post('name');
				$info['name_ascii']		=	ascii_link($info['name']);
				$info['directory_name']	=	ascii_link($info['name']);
				$info['description']	=	$this->input->post('description');
				$info['order']			=	$this->input->post('order');
				$info['created_date']	=	date('Y-m-d');
				if($this->group_model->check_exist($info['name'])) {
					$data['error'] 		= 	'This name is exist!';	//	 Should change with $this->lang->line('name exist')
				}elseif ($this->group_model->check_order_exist($info['order'])){
					$data['error'] 		= 	'This order is exist !';
				}else{
					// insert into DB
					$res				=	$this->group_model->add($info);
					
					// create folder.
					$data['success'] 	= 	'Added Successfully !';
				}
			}else {
				$data['error']	=	"Oops! Album can not create. Please try again.";
			}
		}
		$data['select']		=	"gallery";
		$data['tpl_file']	=	"add_album";
		$this->load->view('admin/layout/index', $data);
	}
	
	
	
	/* function edit($id = 0){
		if($_SERVER['REQUEST_METHOD'] == "POST"){
			$this->form_validation->set_rules('name', $this->lang->line('name'), 'required|trim|xss_clean');
			$this->form_validation->set_rules('description', $this->lang->line('description'), 'trim|xss_clean');
			if($this->form_validation->run() == TRUE){
				$info					=	array();
				$info['name']			=	$this->input->post('name');
				$info['description']	=	$this->input->post('description');
				$info['order']			=	$this->input->post('order');
				if($this->group_model->check_exist_edit($id, $info['name'])) {
					die('This name is exist!');	//	 Should change with $this->lang->line('name exist')
				}elseif ($this->group_model->check_order_exist_edit($id, $info['order'])){
					die('This order is exist !');
				}else{
					$res	=	$this->group_model->edit($id, $info);
					die('yes');
				}
			}else {
				die(validation_errors());
			}
		}
		$data['current_url']=	$this->session->userdata('group_url');
		$data['info']		=	$this->group_model->get_match($id);
		$this->load->view('edit-cate', $data);
	}
	
	
	function delete($id = 0){
		#delete images belong to the category.
		$images	=	$this->group_model->get_images($id);
		foreach ($images as $k => $v){
			if(file_exists($v->image_url)){unlink($v->image_url);}
			if(file_exists($v->thumb_url)){unlink($v->thumb_url);}
			$this->group_model->delete_images($id);
		}
		
		#delete in DB.
    	$this->group_model->delete($id);
     
	    $delete	=	"delete group";
	    $this->session->set_userdata('delete_group', $delete);
	    
	    $url	=	$this->session->userdata('group_url');
	    redirect($url,'refresh');
	}
	
	
	
	function change_order() {
		$group_id 		= 	$this->input->post('group_id');
		$group_order 	= 	$this->input->post('group_order');
	
		$video 			= 	$this->group_model->get_match($group_id); //cate will be changed order position.
		$order		 	= 	$this->group_model->get_match_order($group_order); // Check 'order' exists or not?
	
		if (count($order) > 0) {
			$this->group_model->update_order($order['id'], array('order' => $video->order));
			$this->group_model->update_order($group_id, array('order' => $group_order));
	
		} else {
			$this->group_model->update_order($group_id, array('order' => $group_order));
		}
	
		$url	=	$this->session->userdata('group_url');
		redirect($url, 'refresh');
	}
	
	
	
	function selfURL(){
		if(!isset($_SERVER['REQUEST_URI'])){
			$serverrequri = $_SERVER['PHP_SELF'];
		}else{
			$serverrequri =    $_SERVER['REQUEST_URI'];
		}
		$protocol 	= strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === FALSE ? 'http' : 'https';
		$host     	= $_SERVER['HTTP_HOST'];
		$port 		= ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
	
	
		$currentUrl = $protocol . '://' . $host . $port .$_SERVER['REQUEST_URI'];
	
		return $currentUrl;
	} */
	

	
}

/*---------------------------------------------End-----------------------------------------*/